﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace EventLogMonitor
{
    public partial class Form1 : Form
    {
        private EventLogEntryCollection eLogEntries;
        private static string Machine = ".";
        private static string LogType = "application";

        private static Thread LogThread = null;
        private delegate void logDeleteGate(ListViewItem item);

        private object thisLock = new object();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            ShowLog();
        }

        private void lvLogList_DoubleClick(object sender, EventArgs e)
        {

        }

        private void rbtnApp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbtnApp.Checked)
            {
                LogType = "application";
                ShowLog();
            }
        }

        private void rbtnSec_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbtnSec.Checked)
            {
                LogType = "security";
                ShowLog();
            }
        }

        private void rbtnSys_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbtnSys.Checked)
            {
                LogType = "system";
                ShowLog();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ShowLog();
        }

        private void ShowLog()
        {
            if (LogThread == null || !LogThread.IsAlive)
            {
                LogThread = new Thread(new ThreadStart(ThreadLog));
                LogThread.Start();
            }
        }

        private void ThreadLog()
        {
            EventLog eLog = null;
            try
            {
                eLog = new EventLog(LogType, Machine);
                eLogEntries = eLog.Entries;
               // int i = 1;
                this.lvLogList.Items.Clear();
                for (int i = eLogEntries.Count; i > 0; i--)
                {
                    ListViewItem item = new ListViewItem(i.ToString());
                    switch (eLogEntries[i - 1].EntryType)
                    {
                        case EventLogEntryType.Error:
                            item.SubItems.Add("错误");
                            break;
                        case EventLogEntryType.Warning:
                            item.SubItems.Add("警告");
                            break;
                        case EventLogEntryType.Information:
                            item.SubItems.Add("信息");
                            break;
                        case EventLogEntryType.FailureAudit:
                            item.SubItems.Add("失败审核");
                            break;
                        case EventLogEntryType.SuccessAudit:
                            item.SubItems.Add("成功审核");
                            break;
                        default:
                            item.SubItems.Add("未知");
                            break;
                    }
                    item.SubItems.Add(eLogEntries[i - 1].TimeGenerated.ToString("yyyy-MM-dd HH:mm:ss"));
                    item.SubItems.Add(eLogEntries[i - 1].Source);
                    item.SubItems.Add(eLogEntries[i - 1].EventID.ToString());
                    item.SubItems.Add(eLogEntries[i - 1].UserName);

                    this.lvLogList.Invoke(new logDeleteGate(DeleGateLog), new object[] { item });
                }
                //foreach (EventLogEntry evt in eLogEntries)
                //{
                //    ListViewItem item = new ListViewItem(eLogEntries. i.ToString());
                //    item.SubItems.Add(evt.EntryType.ToString());
                //    item.SubItems.Add(evt.TimeGenerated.ToString("yyyy-MM-dd HH:mm:ss"));
                //    item.SubItems.Add(evt.Source);
                //    item.SubItems.Add(evt.EventID.ToString());
                //    item.SubItems.Add(evt.UserName);

                //    this.lvLogList.Invoke(new logDeleteGate(DeleGateLog), new object[] { item });
                //    i++;
                //}
            }
            catch (InvalidOperationException)
            {
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                eLog.Dispose();
            }
        }

        private void DeleGateLog(ListViewItem item)
        {
            lock (thisLock)
            {
                this.lvLogList.Items.Add(item);
            }
        }

        public class CustomListView : ListView
        {
            public CustomListView()
            {
                SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
                UpdateStyles();
            }
        }
    }
}
